package com.easyattendance.services;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.easyattendance.model.Assignment;
import com.easyattendance.model.Material;
import com.easyattendance.model.MaterialClass;
import com.easyattendance.model.MaterialSection;
import com.easyattendance.model.Teacher;
import com.easyattendance.util.HibernateUtil;

public class AddMaterialService {
	
	/* Method to add a material uploaded by teacher in the database */
	   public synchronized boolean addMaterialForClass(Material material,MaterialClass materialClass){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();

	         Set<MaterialClass> material_class=new HashSet<MaterialClass>();
	         material_class.add(materialClass);
	         material.setMaterialClass(material_class);
	         session.save(material);
	         session.save(materialClass);
	        	 
	         tx.commit();
	         result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	     return result;
	   }
	   
	   /* Method to add a material uploaded by teacher in the database */
	   public synchronized boolean addMaterialForSection(Material material,MaterialSection materialSection){
	      Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean result=false;
	      
	      try{
	         tx = session.beginTransaction();

	         Set<MaterialSection> material_section=new HashSet<MaterialSection>();
	         material_section.add(materialSection);
	         material.setMaterialSection(material_section);
	         session.save(material);
	         session.save(materialSection);
	        	 
	         tx.commit();
	         result=true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	     return result;
	   }
	   
	   /* Method to DELETE material from the records */
	   public synchronized boolean deleteMaterial(String materialId){
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean flag = false;
	      try{
	         tx = session.beginTransaction();
	         Material material = (Material) session.createQuery("FROM Material where material_id='"+materialId+"' ").uniqueResult();
	         session.delete(material);
	         
	         tx.commit();
	         flag = true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return flag;
	   }
	   
	   /* Method to DELETE material uploaded by teacher for particular class and subject */
	   public synchronized boolean deleteFromMaterialClass(String materialId,String classId,String subjectId){
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean flag = false;
	      try{
	         tx = session.beginTransaction();
	        
	         MaterialClass material_class = (MaterialClass) session.createQuery("FROM MaterialClass where material_id='"+materialId+"' and class_id='"+classId+"' and subject_id='"+subjectId+"' ").uniqueResult();
	         session.delete(material_class);
	         
	         tx.commit();
	         flag = true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return flag;
	   }
	   
	   /* Method to DELETE material uploaded by teacher for particular class and subject */
	   public synchronized boolean deleteFromMaterialSection(String materialId,String sectionId){
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      boolean flag = false;
	      try{
	         tx = session.beginTransaction();
	        
	         MaterialSection material_section = (MaterialSection) session.createQuery("FROM MaterialSection where material_id='"+materialId+"' and section_id='"+sectionId+"' ").uniqueResult();
	         session.delete(material_section);
	         
	         tx.commit();
	         flag = true;
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return flag;
	   }
	   
	   public boolean ifAlreadyExists(Material material) {
			 boolean flag = false;
			 
			 Session session = HibernateUtil.openSession();
		      Transaction tx = null;
		      List materialList = null;
		      try{
		         tx = session.beginTransaction();
		         materialList = session.createQuery("FROM Material").list(); 
		         for (Iterator iterator = materialList.iterator(); iterator.hasNext();){
		        	 Material tempMaterial = (Material) iterator.next(); 
		            if(tempMaterial.equals(material)) {
		            	System.out.println("Material already uploaded");
		            	flag = true;
		            	break;
		            }
		             
		         }
		         tx.commit();
		      }catch (HibernateException e) {
		         if (tx!=null) tx.rollback();
		         e.printStackTrace(); 
		      }finally {
		         session.close(); 
		      }
			 
			 return flag;
		}
	   
	 
	   
	   /* Method to  READ all the material records */
	   public List listMaterials( ) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List materials = null;
	      try{
	         tx = session.beginTransaction();
	         materials = session.createQuery("FROM Material").list(); 
	         for (Iterator iterator =materials.iterator(); iterator.hasNext();){
	        	Material tempmaterial = (Material) iterator.next(); 
	            System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materials;
	   }
	   
	   /* Method to  READ all the materials uploaded by particular teacher */
	  /* public List<Material> listMaterials(String currentUser) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<Material> materials = null;
	     
	      try{
	         tx = session.beginTransaction();
	         Query query = session.createQuery("from Teacher where email_id='"+currentUser+"'");
	         Teacher teacher = (Teacher)query.uniqueResult();
	         materials = session.createQuery("FROM Material where uploaded_by='"+teacher.getTeacher_name()+" "+teacher.getTeacher_lname()+"'").list(); 
	         for (Iterator iterator = materials.iterator(); iterator.hasNext();){
	        	Material tempmaterial = (Material) iterator.next(); 
	            System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materials;
	   } */
	   
	   /* Method to  READ all the materials uploaded by particular teacher from MaterialClass*/
	   public List<MaterialClass> listMaterialsByClass(String currentUser) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<Material> materials = null;
	      List<MaterialClass> material_class=new ArrayList<MaterialClass>();
	      Query query=null;
	      MaterialClass materialClass=new MaterialClass();
	      
	      try{
	         tx = session.beginTransaction();
	         query = session.createQuery("from Teacher where email_id='"+currentUser+"'");
	         Teacher teacher = (Teacher)query.uniqueResult();
	         materials = session.createQuery("FROM Material where uploaded_by='"+teacher.getTeacher_name()+" "+teacher.getTeacher_lname()+"'").list(); 
	         for (Iterator iterator = materials.iterator(); iterator.hasNext();){
	        	Material tempmaterial = (Material) iterator.next(); 
	        	query=session.createQuery("from MaterialClass where material_id='"+tempmaterial.getMaterial_id()+"'");
	        	materialClass=(MaterialClass) query.uniqueResult();
	        	material_class.add(materialClass);
	            System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return material_class;
	   }


	   /* Method to  READ all the materials uploaded by particular section */
	   public List<MaterialSection> listMaterialsBySection(String sectionName) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<Material> materials = null;
	      List<MaterialSection> materialSectionList=new ArrayList<MaterialSection>();
	      Query query=null;
	      MaterialSection materialSection=new MaterialSection();
	      
	      try{
	         tx = session.beginTransaction();
	         materials = session.createQuery("FROM Material where uploaded_by='"+sectionName+"'").list(); 
	         for (Iterator iterator = materials.iterator(); iterator.hasNext();){
	        	Material tempmaterial = (Material) iterator.next(); 
	        	query=session.createQuery("from MaterialSection where material_id='"+tempmaterial.getMaterial_id()+"' ");
	            materialSection=(MaterialSection) query.uniqueResult();
	            materialSectionList.add(materialSection);
	        	System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materialSectionList;
	   }
	   
	   /* Method to  READ all the material records uploaded for particular subject */
	   public List<MaterialClass> listMaterialsBySubject(String subjectId,String classId) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<MaterialClass> materials = null;
	      try{
	         tx = session.beginTransaction();
	        
	         materials = session.createQuery("FROM MaterialClass where subject_id='"+subjectId+"' and class_id='"+classId+"'").list(); 
	         for (Iterator iterator = materials.iterator(); iterator.hasNext();){
	        	MaterialClass tempmaterial = (MaterialClass) iterator.next(); 
	            System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materials;
	   }
	   
	   /* Method to  READ all the syllabus uploaded for particular subject */
	   public List<Material> listSyllabusBySubject(String subjectId,String classId) {
		  Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List PathList= null,NameList=null;
	      //List materials=null;
	      List<Material> materialList=new ArrayList<Material>();
	      String paths[] =null ,material_names[] = null;
	    
	      try{
	         tx = session.beginTransaction();
	        
	         NameList = session.createSQLQuery("Select material.material_name FROM tbl_material as material,tbl_material_class as material_class where material_class.subject_id='"+subjectId+"'"
	         		+ " and material_class.class_id='"+classId+"' and material.material_type='Syllabus' and material.material_id=material_class.material_id").list(); 
	       
	         PathList= session.createSQLQuery("Select material.physical_path FROM tbl_material as material,tbl_material_class as material_class where material_class.subject_id='"+subjectId+"'"
		         		+ " and material_class.class_id='"+classId+"' and material.material_type='Syllabus' and material.material_id=material_class.material_id").list(); 
	         
	         paths=new String[PathList.size()];
	         material_names=new String[NameList.size()];
	         
	         for(int i=0;i<PathList.size();i++)
	         {
	        	  paths[i]=PathList.get(i).toString();
	         }
	         
	         for(int i=0;i<NameList.size();i++)
	         {
	        	 material_names[i]=NameList.get(i).toString();
	        	 materialList.add(new Material(material_names[i],paths[i]));
	         }
	        
	         
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materialList;
	   }
	   
	   /* Method to  READ all the circulars uploaded by particular section */
	   public List<MaterialSection> listSectionMaterialsBySection_Class(String sectionId,String classId) {
		   Session session = HibernateUtil.openSession();
	      Transaction tx = null;
	      List<MaterialSection> materials = null;
	      try{
	         tx = session.beginTransaction();
	        
	         materials = session.createQuery("FROM MaterialSection where section_id='"+sectionId+"' and (class_id='"+classId+"' or class_id='') ").list(); 
	         for (Iterator iterator = materials.iterator(); iterator.hasNext();){
	        	MaterialSection tempmaterial = (MaterialSection) iterator.next(); 
	            System.out.print("MaterialId: " + tempmaterial.getMaterial_id()); 
	             
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	      return materials;
	   }
}
